package com.woniuxy.shop.servelet.front;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.woniuxy.shop.entity.Cart;
import com.woniuxy.shop.entity.Goods;
import com.woniuxy.shop.entity.ResponseEntity;
import com.woniuxy.shop.entity.User;
import com.woniuxy.shop.service.CartService;
import com.woniuxy.shop.service.GoodsService;
import com.woniuxy.shop.service.ServiceProxyFactory;
import com.woniuxy.shop.utils.ShopUtils;

/**
 * Servlet implementation class CateGoryServelet
 */
@WebServlet("/cart")
public class CartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	CartService cs = ServiceProxyFactory.getProxy(CartService.class);
	GoodsService gs = new GoodsService();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String opr = request.getParameter("opr");
		if ("add".equals(opr)) {
			doAdd(request, response);
		}else if("in".equals(opr)){
			doIn(request, response);
		}else if("del".equals(opr)){
			doDel(request, response);
		}

	}

	
	private void doDel(HttpServletRequest request, HttpServletResponse response) throws IOException {
		ResponseEntity<Void> rese = new ResponseEntity<Void>(); 
		try {
			int cartid = Integer.parseInt(request.getParameter("cartid"));
			cs.del(cartid);
		} catch (Exception e) {
			rese.setCode(101);
			rese.setMsg("erro");
		}
		rese.setCode(200);
		rese.setMsg("ok");
		ShopUtils.write(response, rese);
		
	}


	/**
	 * 进入购物车准备数据
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void doIn(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		ResponseEntity<List<Cart>> rese =  new ResponseEntity<>();
		List<Cart> carts = null;
		try {
			User u = (User)(request.getSession().getAttribute("user"));
			if(u==null) {
				//若a为null则表明没有登录
				response.sendRedirect("login.jsp");
				return;
			}
			
			carts = cs.getCartsByUser(u.getId());
			if(carts!=null) {
				for (Cart cart : carts) {
					//获取每个购物车对应的商品
					Goods g = gs.getGoods(cart.getGoodsid());
					//将商品赋值到购物车
					cart.setGoods(g);
				}
			}
		} catch (Exception e) {
			rese.setCode(101);
			rese.setMsg("error");
			ShopUtils.write(response, rese);
		}
		rese.setCode(200);
		rese.setMsg("ok");
		rese.setData(carts);
		ShopUtils.write(response, rese);
		
	}



	/**
	 * 增加购物车
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void doAdd(HttpServletRequest request, HttpServletResponse response) throws IOException {
		ResponseEntity<String> rese = new ResponseEntity<String>();
		try {
			int goodsid = Integer.parseInt(request.getParameter("goodsid"));
			User u = (User) request.getSession().getAttribute("user");
			int userid = u.getId();
			int nums = Integer.parseInt(request.getParameter("goodsnum"));
			double price = Double.parseDouble(request.getParameter("price"));
			String addtime = ShopUtils.getNowTimeWithType();
			Cart c = new Cart();
			c.setUserid(userid);
			c.setGoodsid(goodsid);
			c.setNums(nums);
			c.setPrice(price);
			c.setAddtime(addtime);
			cs.add(c);
		} catch (Exception e) {
			rese.setCode(500);
			rese.setData("error");
			rese.setMsg("error");
		}
		
		rese.setCode(200);
		rese.setData("ok");
		rese.setMsg("ok");
		ShopUtils.write(response, rese);
		
		
		
	}




	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
